package org.example.service.impl;

import org.example.entity.User;
import org.example.exception.AccountNotFoundException;
import org.example.exception.PasswordErrorException;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(User user) {
        String userName=user.getUserName();
        String password=user.getPassword();
        User user1=userMapper.getByUserName(userName);
        if (user1==null){
            throw new AccountNotFoundException("账户不存在");
        }
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user1.getPassword())){
            throw new PasswordErrorException("密码错误");
        }

        return user1;
    }
}
